package core;

import org.apache.commons.collections.MapUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public class ApplicationContext {
	protected List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();//用来存储所有的beans
	protected Map<String, Object> sigletons = new HashMap<String, Object>();//用来存储实例化后的bean
	/**
	 * 完成实例化beans
	 *
	 * Administer
	 * 2013-8-18 上午1:07:51
	 */
	protected void instanceBeans() {
		if (beanDefines != null && beanDefines.size() >0) {
			List<SpringMethod> springMethods = new ArrayList<SpringMethod>();
			//对每个bean进行实例化
			for (BeanDefinition beanDefinition : beanDefines) {
				try {
					//bean的class属性存在的时候才进行实例化，否则不进行实例化
					if (beanDefinition.getClassName() != null && !beanDefinition.getClassName().equals("")) {

						//实例化的关键操作
						Class<?> aClass1 = Class.forName(beanDefinition.getClassName());
						Object o = aClass1.newInstance();
						if (MapUtils.isNotEmpty(beanDefinition.stringBeanDefinitionMap)) {

							Map<String, String> stringBeanDefinitionMap = beanDefinition.stringBeanDefinitionMap;
							Set<Map.Entry<String, String>> entries = stringBeanDefinitionMap.entrySet();
							Iterator<Map.Entry<String, String>> iterator = entries.iterator();
							while (iterator.hasNext()) {
								Map.Entry<String, String> next = iterator.next();
								String parameter = next.getValue();
								String key = next.getKey();
								String methodName = getMethodName(key);


								Object parameterObject = sigletons.get(parameter);

								if (parameterObject!=null) {
									Method setCar = aClass1.getMethod(methodName, parameterObject.getClass());
									setCar.invoke(o, sigletons.get(parameter));
								}else{
									//参数没有spring管理的暂存,后续注入
									springMethods.add(new SpringMethod(o,key,parameter));
								}
							}
						}
						sigletons.put(beanDefinition.getId(), o);

						System.out.println("id为："+beanDefinition.getId()+"的bean实例化成功");
					}
				} catch (Exception e) {
					System.out.println("bean实例化失败");
					e.printStackTrace();
				}
			}
			for (SpringMethod springMethod : springMethods) {
				//没有注入的参数.注入
				try {
					Object object = springMethod.object;
					String methodName = getMethodName(springMethod.property);
					Object o = sigletons.get(springMethod.parameterName);
					Method method = object.getClass().getMethod(methodName, o.getClass());
					method.invoke(object, o);
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}

			}
		}
	}
	private String getMethodName(String key) {
		char c = key.charAt(0);
		String s = c + "";
		String s1 = s.toUpperCase();
		return "set" + s1 + key.substring(1);
	}

	/**
	 * 通过bean名称来获取bean对象
	 * @param beanName
	 * @return
	 * Administer
	 * 2013-8-18 上午1:17:02
	 */
	public Object getBean(String beanName){
		return sigletons.get(beanName);
	}
}
